package com.zzy.librarytest.web;

import com.zzy.librarytest.entity.Book;
import com.zzy.librarytest.entity.User;
import com.zzy.librarytest.service.BookServvice;
import com.zzy.librarytest.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 用户操作
 */
@Controller
public class UserController {
    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private BookServvice bookServvice;
    @PostMapping("/login")
    public String login(String username , String password, HttpSession httpSession, Model model){
        Integer integer = userService.selectUser(username, password);
        if (integer!=null){
            httpSession.setAttribute("msg","登录成功");
            return "redirect:homepage";
        }else {
            model.addAttribute("msg","账号密码错误");
            return "index";
        }
    }
    @GetMapping("/homepage")
    public String homepage(Model model){
        List<Book> books = bookServvice.selectAllBook();
        model.addAttribute("books",books);
        return "homepage";
    }

    @GetMapping("/homepage/exit")
    public String exit(HttpSession session){
        session.removeAttribute("msg");
        return "index";
    }

    @PostMapping("/registeruser")
    public String register(String username,String password,Model model){
        User integer1 = userService.selectOneUser(username);
        if (integer1==null){
            Integer integer = userService.insertUser(username, password);
            model.addAttribute("msg","创建 "+username+"成功 请登录");
            return "index";
            }else {
                model.addAttribute("msg","用户名存在 创建失败");
                return "user/registeruser";
            }
    }

    @GetMapping("/homepage/selectoneuser")
    public String seleconetuser(String username,Model model){
        User user = userService.selectOneUser(username);
        if (user!=null){
            model.addAttribute("user",user);
            return "user/amenduser";
        }else {
            model.addAttribute("msg","没有这个用户");
            return "user/amenduser";
        }
    }

    @PostMapping("/homepage/updateuser")
    public String updateuser(String username,String password,Model model){
        Integer integer1 = userService.updateUser(username,password);
        if (integer1!=null){
            User user = userService.selectOneUser(username);
            model.addAttribute("user",user);
            model.addAttribute("msg","修改成功");
            return "user/amenduser";
        }else {
            model.addAttribute("msg","用户名存在 修改失败");
            return "user/registeruser";
        }
    }

    @GetMapping("/homepage/selectoneuser2")
    public String selectoneuser2(String username,Model model){
        User user = userService.selectOneUser(username);
        if (user!=null){
            model.addAttribute("user",user);
            return "user/deleteuser";
        }else {
            model.addAttribute("msg","没有这个用户");
            return "user/deleteuser";
        }
    }

    @PostMapping("/homepage/deleteuser")
    public String deleteuser(String username,String password,Model model){
        Integer integer1 = userService.deleteUser(username);
        if (integer1!=null){
            model.addAttribute("msg","删除"+username+"成功");
            return "user/deleteuser";
        }else {
            model.addAttribute("msg","删除失败");
            return "user/deleteuser";
        }
    }

    @GetMapping("/homepage/alluser")
    public String selectalluser(Model model){
        List<User> users = userService.selectAllUser();
        model.addAttribute("users",users);
        return "user/alluser";
    }


}
